<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动画帧</title>
    <style>
      div {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 80px;
        height: 80px;
        margin: 80px auto;
        background-color: skyblue;
        transition: all 0.6s;
      }

      /* 进场动画 */
      .aniIn {
        animation: Test 0.6s ease-in-out 0s forwards;
      }

      @keyframes Test {
        /* from等价于0%,to等价于100% */
        to {
          transform: rotate(180deg);
        }
      }

      /* 出场动画 */
      .aniOut {
        animation: Test2 0.6s ease-in-out 0s forwards;
      }

      @keyframes Test2 {
        /* from等价于0%,to等价于100% */
        from {
          transform: rotate(180deg);
        }
        to {
          transform: rotate(0deg);
        }
      }
    </style>
  </head>
  <body>
    <div id="box">123</div>
    <script>
      // 鼠标进入事件
      document
        .getElementById("box")
        .addEventListener("mouseenter", function () {
          this.classList.add("aniIn");
        });

      // 鼠标离开事件
      document
        .getElementById("box")
        .addEventListener("mouseleave", function () {
          this.classList.add("aniOut");
          setTimeout(() => {
            this.classList.remove("aniIn");
            this.classList.remove("aniOut");
          }, 400);
        });
    </script>
  </body>
</html>
